package com.syni.mdd.yhd.common.all.quartz.groupMessage;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessGroupMessage;
import com.syni.mdd.yhd.common.all.service.BmsBusinessGroupMessageService;
import com.syni.mdd.sjb.common.component.utils.common.utils.SpringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @className GroupMessageJobListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/6/27 14:14
 * @VERSION 1.0
 **/
public class GroupMessageJobListener implements JobListener {
    private String jobCallbackId;

    public GroupMessageJobListener() {
    }

    public GroupMessageJobListener(String jobCallbackId) {
        this.jobCallbackId = jobCallbackId;
    }

    @Override
    public String getName() {
        return getClass().getName() + "_" + jobCallbackId;
    }

    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {

    }

    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {

        System.out.println("b？");
    }

    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {
        BmsBusinessGroupMessageService bmsBusinessGroupMessageService = (BmsBusinessGroupMessageService)SpringUtils.getBean("bmsBusinessGroupMessageService");
        Map<String, Object> paramsMap = jobExecutionContext.getJobDetail().getJobDataMap();
        BmsBusinessGroupMessage bmsBusinessGroupMessage = (BmsBusinessGroupMessage) paramsMap.get("bmsBusinessGroupMessage");
        if(e == null) {
            bmsBusinessGroupMessage.setStatus(Constants.COMMON_FLAG_TRUE_INT);
        }else {
            bmsBusinessGroupMessage.setStatus(BmsBusinessGroupMessageService.SEND_FAIL);
        }
        bmsBusinessGroupMessageService.save(bmsBusinessGroupMessage);
    }
}
